package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectCount;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.AddAccountDto;
import com.woniu.defecttracking.dto.FindAccountDto;
import com.woniu.defecttracking.dto.PageAccountDto;
import com.woniu.defecttracking.entity.Account;
import com.woniu.defecttracking.entity.AccountDetails;
import com.woniu.defecttracking.mapper.AccountMapper;
import com.woniu.defecttracking.param.AddAccountParam;
import com.woniu.defecttracking.param.FindAccountParam;
import com.woniu.defecttracking.param.UpdateAccountParam;
import com.woniu.defecttracking.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.defecttracking.service.ProjectAccountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private ProjectAccountService projectAccountService;

    @Override
    public PageAccountDto findAccount(FindAccountParam findAccountParam) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        System.out.println(findAccountParam.getKeywords());
        //模糊查询
        if (findAccountParam.getKeywords()!=null){
            queryWrapper.and(qw ->{
                qw.like("account_username",findAccountParam.getKeywords()).or()
                        .like("account_name",findAccountParam.getKeywords());
            });

        }

        queryWrapper.eq("account_isdel",0);
        //排序
        queryWrapper.orderByDesc("account_id");
        //分页
        Page<Account> objectPage = new Page<>();
        objectPage.setCurrent(findAccountParam.getPageIndex());
        objectPage.setSize(findAccountParam.getPageSize());
        Page<Account> page = page(objectPage, queryWrapper);

        PageAccountDto pageAccountDto = new PageAccountDto();
        pageAccountDto.setTotal(page.getTotal());
        pageAccountDto.setIndexPage(page.getCurrent());

        List<Account> records = page.getRecords();


        List<FindAccountDto> findAccountDtos = ComUtil.copyList(records, FindAccountDto.class);

        pageAccountDto.setFindAccountDtos(findAccountDtos);

        return pageAccountDto;
    }
    @AddNum("accountNum")
    @Override
    public AddAccountDto addAccount(AddAccountParam addAccountParam) {

        if (addAccountParam==null){
            return null;
        }
        Account account = ComUtil.copyProperties(addAccountParam, Account.class);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123");
        account.setAccountPassword(encode);
        account.setAccountIsdel(0);
        if (saveOrUpdate(account)){
            AddAccountDto addAccountDto = ComUtil.copyProperties(account, AddAccountDto.class);

            return addAccountDto;

        }
        return null;
    }

    @Override
    public boolean updateAccount(UpdateAccountParam updateAccountParam) {

        Account account = ComUtil.copyProperties(updateAccountParam, Account.class);
        if (account.getAccountPassword()!=null){
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String password = encoder.encode(account.getAccountPassword());
            account.setAccountPassword(password);
        }
        Integer accountId = account.getAccountId();
        Account byId = getById(accountId);
        BeanUtils.copyProperties(account,byId);

        if (updateById(byId)){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteAccount(List<Integer> ids) {
        List<Account> accounts = listByIds(ids);
        if (ids.size()==accounts.size()){
            for (Account account:accounts){
                account.setAccountIsdel(1);
            }
           return saveOrUpdateBatch(accounts);
        }
        return false;
    }



    @Override
    public List<Account> findProjectId(Integer id) {

        List<Account> accounts = this.baseMapper.findProjectId(id);
        return accounts;
    }

    @Override
    public boolean toSuper(Integer id) {
        Account byId = getById(id);
        if (byId.getAccountSupper()==0) {
            byId.setAccountSupper(1);
        }else {
            byId.setAccountSupper(0);
        }
        saveOrUpdate(byId);
        return true;
    }

    @Override
    public boolean isSuper() {
        AccountDetails principal = (AccountDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer id = principal.getAccountId();
        Account byId = getById(id);
        if (byId.getAccountSupper()==0)
        return false;
        else
            return true;
    }
}
